﻿using Microsoft.Extensions.DependencyModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Webapi.EFCore.Common.Helper
{
    public static class DependencyUtilities
    {
        const string pattern = "^Microsoft.\\w*|^System.\\w*|^Newtonsoft.\\w*|^Autofac.\\w*|^Serilog.\\w*|^App.\\w*|^runtime.\\w*|^Google.\\w*|^IdentityServer4.\\w*";

        public static Assembly[] GetReferencedAssemblies()
        {
            Regex relatedRegex = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase);
            return DependencyContext.Default.RuntimeLibraries
                 .Where(item => !relatedRegex.IsMatch(item.Name))
                 .SelectMany(item => item.GetDefaultAssemblyNames(DependencyContext.Default))
                 .Select(item => Assembly.Load(item))
                 .ToArray();
        }
    }
}
